//
//  CurrentCityView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/13.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol CurrentCityViewDelegate {
    @objc optional func selectCurrentCity(name:String) //选择当前城市
}

class CurrentCityView: UIView {

    weak var currentDelegate : CurrentCityViewDelegate?
    
    fileprivate let cityButton = UIButton()
    
    var text : String! {
        didSet{
            cityButton.setTitle(text, for: .normal)
        }
    }
    
    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.clear
        
        let titleLabel = UILabel()
        titleLabel.text = currentCity
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        self.addSubview(titleLabel)
        
        cityButton.backgroundColor = UIColor.white
        cityButton.setTitleColor(UIColor.black, for: .normal)
        cityButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        cityButton.addTarget(self, action: #selector(selectCurrentCity), for: .touchUpInside)
        self.addSubview(cityButton)
        
        let grayView = UIView()
        grayView.backgroundColor = UIColor.lineColor()
        self.addSubview(grayView)
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 8))
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        cityButton.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.width.equalTo(RATIO(maxNum: 100))
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        grayView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //AMRK: 选择当前城市
    @objc fileprivate func selectCurrentCity() {
    
        self.currentDelegate?.selectCurrentCity!(name: self.text)
    }
    
}
